/*******************************************************************************
 * DIALOG WINDOW
 * 
 * Copyright:
 * 
 * License:
 * 
 * Authors: SALMON Charles
 * 
 ******************************************************************************/

/*******************************************************************************
 * 
 ******************************************************************************/

qx.Class.define("bip.ui.win.Dialog", {
	extend : qx.ui.window.Window,

	include : [ bip.ui.form.MActionBar ],

	statics : {
		readyIconPath : "icon/16/status/dialog-information.png",
		warningIconPath : "icon/16/status/dialog-warning.png",
		errorIconPath : "icon/16/status/dialog-error.png",
		runningIconPath : "bip/icon/loading.gif",
		okIconPath : "icon/16/actions/dialog-apply.png"
	},

	/*
	 * ****************************************************************************
	 * EVENTS
	 * ****************************************************************************
	 */
	events : {

	},

	/*
	 * ****************************************************************************
	 * PROPERTIES
	 * ****************************************************************************
	 */
	properties : {
		// overridden
		appearance : {
			refine : true,
			init : "wdialog"
		},

		/** The header image */
		headerImage : {
			apply : "_applyHeaderImage",
			check : "String",
			nullable : true
		},

		/** The header title */
		headerTitle : {
			apply : "_applyHeaderTitle",
			check : "String",
			init : "<header title>"
		},

		/** The header description */
		headerDescription : {
			apply : "_applyHeaderDescription",
			check : "String",
			init : "<header description>"
		},

		state : {
			check : [ "ready", "running", "warning", "error", "ok" ],
			init : "ready",
			apply : "__applyState",
			event : "changeState"
		},

		/**
		 * Whether to block the ui while the widget is displayed
		 */
		useBlocker : {
			check : "Boolean",
			init : true
		},

		/**
		 * The blocker's color
		 */
		blockerColor : {
			check : "String",
			init : "black"
		},

		/**
		 * The blocker's opacity
		 */
		blockerOpacity : {
			check : "Number",
			init : 0.5
		}
	},

	/*
	 * ****************************************************************************
	 * CONSTRUCTOR
	 * ****************************************************************************
	 */

	/**
	 * Construct the window
	 * 
	 * @param title
	 *          {String}
	 * @param headerTitle
	 *          {String}
	 * @param headerDescription
	 *          {String}
	 * @param headerImage
	 *          {String}
	 */
	construct : function(title, headerTitle, headerDescription, headerImage) {
		this.base(arguments, title);
		this.set({
			width : 800,
			height : 600
		});

		/*
		 * appear event
		 */
		this.addListenerOnce("appear", function(e) {
			this.center();
		}, this);

		// apply constructor parameters
		if (headerTitle != null) {
			this.setHeaderTitle(headerTitle);
		} else {
			if (title != null) {
				this.setHeaderTitle(title);
			}
		}
		if (headerDescription != null) {
			this.setHeaderDescription(headerDescription);
		}
		if (headerImage != null) {
			this.setHeaderImage(headerImage);
		}

		/*
		 * create widget content
		 */
		this._createWidgetContent();

	},

	/*
	 * ****************************************************************************
	 * MEMBERS
	 * ****************************************************************************
	 */
	members : {

		/**
		 * Set the window in the specified state and update the headerDescription
		 * with the given text
		 */
		setStateWithDescription : function(state, description) {
			this.setState(state);
			this.setHeaderDescription(description);
		},

		/*
		 * ****************************************************************************
		 * UI
		 * ****************************************************************************
		 */
		// overridden
		_createChildControlImpl : function(id) {
			var control;
			switch (id) {
			/*
			 * case "pane": var layout = new qx.ui.layout.VBox(); control = new
			 * qx.ui.container.Composite(layout); this._add(control, {flex: 1});
			 * break;
			 */
			case "header":
				var layout = new qx.ui.layout.Grid();
				layout.setRowFlex(1, 1);
				layout.setColumnFlex(0, 1);
				control = new qx.ui.container.Composite(layout);
				this._addBefore(control, this.getChildControl("pane"));

				break;

			case "header-image":
				control = new qx.ui.basic.Image();
				this.getChildControl("header").add(control, {
					row : 0,
					column : 1,
					rowSpan : 2
				});
				break;

			case "header-description":
				control = new qx.ui.basic.Atom();
				control.setWidth(0);
				control.setAllowGrowX(true);

				this.getChildControl("header").add(control, {
					row : 1,
					column : 0
				});
				break;

			case "header-title":
				control = new qx.ui.basic.Label();
				control.setWidth(0);
				control.setAllowGrowX(true);

				this.getChildControl("header").add(control, {
					row : 0,
					column : 0
				});
				break;

			case "actionbar":
				control = new qx.ui.container.Composite(new qx.ui.layout.HBox(10));
				control.set({
					height : 30
				});
				control.add(new qx.ui.core.Spacer(), {
					flex : 1
				});
				this._addAfter(control, this.getChildControl("pane"));

				break;

			}

			return control || this.base(arguments, id);

		},

		/**
		 * Create the content of the dialog. Extending classes must implement this
		 * method.
		 */
		_createWidgetContent : function() {
			this.error("_createWidgetContent not implemented!");
		},

		/**
     * Opens the window.
     *
     * @param props {Map} Properties
     * @return {void}
     */
    open : function(props) {
    	this._beforeShow(props);
    	this.base(arguments);
    },

		_beforeShow : function(props) {
			this.setStateWithDescription("ready", "description");
		},

		/**
		 * Hide the widget. Overriding methods must call this parent method
		 */
		hide : function() {
			this.setVisibility("hidden");
			if (this.isUseBlocker()) {
				this.getApplicationRoot().unblockContent();
			}
			if (this.__previousFocus) {
				try {
					this.__previousFocus.focus();
				} catch (e) {
				}
			}
		},

		/*
		 * ---------------------------------------------------------------------------
		 * PROPERTY APPLY ROUTINES
		 * ---------------------------------------------------------------------------
		 */

		// Header image
		_applyHeaderImage : function(value, old) {
			this.getChildControl("header-image").setSource(value);
		},

		// Header description
		_applyHeaderDescription : function(value, old) {
			this.getChildControl("header-description").setLabel(value);
		},

		// Header title
		_applyHeaderTitle : function(value, old) {
			this.getChildControl("header-title").setValue(value);
		},

		__applyState : function(value, old) {
			switch (value) {
			case "running":
				this.getChildControl("header-description").setIcon(this.self(arguments).runningIconPath);
				this.setHeaderDescription("Running, please wait ...");
				//this.block();
				break;
			case "ok":
				this.getChildControl("header-description").setIcon(this.self(arguments).okIconPath);
				this.setHeaderDescription("OK !");
				//this.forceUnblock();
				break;
			case "warning":
				this.getChildControl("header-description").setIcon(this.self(arguments).warningIconPath);
				this.setHeaderDescription("Warning !");
				//this.forceUnblock();
				break;
			case "error":
				this.getChildControl("header-description").setIcon(this.self(arguments).errorIconPath);
				this.setHeaderDescription("Error !");
				//this.forceUnblock();
				break;
			case "ready":
			default:
				this.getChildControl("header-description").setIcon(this.self(arguments).readyIconPath);
				this.setHeaderDescription("Ready");
				//this.forceUnblock();
				break;
			}
		}
	}

});